package com.passive.lock.controller.key;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passive.lock.controller.BaseController;
import com.passive.lock.model.po.TKey;
import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TKeyVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.key.KeyService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("ke")
public class KeyController extends BaseController {
	@Autowired
	private KeyService keyService;
	
	/**
	 * 钥匙信息管理
	 *
	 */

	@RequestMapping(value = "/keylist")
	@ResponseBody
	public Result keyList(@RequestParam(required = false) String deptId, @RequestParam(required = false) String keyName,
			@RequestParam(required = false) String userName, @RequestParam(required = false) String keyNote,
			@RequestParam(required = false) String keyState, @RequestParam Integer page, @RequestParam Integer limit,HttpServletRequest req, HttpServletResponse resp) {
		try {
			TUserVo tUser = (TUserVo) req.getSession().getAttribute("tUserVo");
			int offset = PageUtil.getOffset(page, limit);
			List<TKey> list = keyService.keyList(tUser.getGroupid(),deptId, keyName, userName, keyNote, keyState, offset, limit);
			int count = keyService.keyListCount(tUser.getGroupid(),deptId, keyName, userName, keyNote, keyState);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addkey")
	@ResponseBody
	public Result addKeySubmit(@RequestBody TKeyVo tKeyVo) {
		try {
			Result result = keyService.addKey(tKeyVo);
			
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updatekey")
	@ResponseBody
	public Result updateKeySubmit(@RequestBody TKeyVo tKeyVo) {
		try {
			Result result = keyService.updateKey(tKeyVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletekey")
	@ResponseBody
	public Result deleteKey(@RequestBody TKeyVo tKeyVo) {
		try {
			Result result = keyService.deleteKey(tKeyVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletekeylist")
	@ResponseBody
	public Result deleteKeyList(@RequestBody List<Integer> tKeyids) {
		try {
			Result result = keyService.deleteKeyList(tKeyids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	@RequestMapping(value = "/querykeylist")
	@ResponseBody
	public Result queryKeyList(@RequestParam(required = false) String deptid,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo user = (TUserVo) req.getSession().getAttribute("tUserVo");
		if(deptid == null||"".equals(deptid)){
			deptid = user.getGroupid();
		}
		try {
			Result result = keyService.queryKeyList(deptid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
}
